import { JSONMapping } from "../json-mapping";
import { ElectricityMeter } from "./electricity-meter";
import { Attachment } from "./attachment";
import { SyncStatus } from "../sync-status";
import {ObjectDtoInterface} from "../../services/sqliteDB/object.dto";

export class Bill extends JSONMapping implements ObjectDtoInterface {
  static PropertyKeyToJSONKeyMap() {
    return {
      attachmentIds: "attachmentIds|attachmentId|noticeImageId",
      thumbnailIds: "thumbnailIds|ThrumnailId|thumnbailId|noticeThrumnailId",
      consumedElectricitys: "consumedElectricitys|meters",
      noticeID: "noticeId|noticeID",
      // noticeAttachments: "noticeAttachments|noticeImageId",

    };
  }

  static JSONValueTranslator(propertyKey) {
    if (propertyKey === "consumedElectricitys") {
      return (electricityMeterJsonObjs: Array<{[key: string]: any}>) => {
        return electricityMeterJsonObjs ? electricityMeterJsonObjs.map((electricityMeterJson) => {
          return new ElectricityMeter(electricityMeterJson);
        }) : null;
      };
    }
    return null;
  }
  // public id: string = null;
  public userId: string = null; // 用户id
  public billDate: number = null; // 时间戳格式
  public year: number = null; // 年份
  public month: number = null; // 月份
  public customerNo: string = null; // 客户号
  public account: string = null; // 客户账户

  public chargeDetailId: string = null; // 催费名单ID和账单id区别？ ------根据欠费记录来的，有欠费就会有chargeDetailId
  public chargeLevel: number = null; // 催费级别
  public chargeStatus: number = null; // 催费状态-----？催费状态，0 非催费 1 处于催费
  public chargeTimesOne: number = null; // 一级催费次数
  public chargeTimesTwo: number = null;	// 二级催费次数
  public chargeTimesThree: number = null;	// 三级催费次数
  public arrearsElectricity: number = null;
  public arrearsBasicFee: number = null;
  public totalFee: number = null;
  public arrearsAdjustFee: number = null;
  public penalty: number = null; // 应收违约金

  public payHistoryId: string = null; // 支付历史Id
  public payMonth: number = null; // 支付月份
  public payDate: number = null; // 支付时间
  public businessType: number = null; // 支付类型
  public businessMethod: string = null; // 支付方式
  public powerUnit: number = null; // 电费
  public actualFee: number = null; // 实收
  public actualPenalty: number = null; // 实收违约金
  public actualTotal: number = null; // 实收总额
  public feeType: string = null;

  public noticeID: string = null; // 通知单Id
  public noticeUploadTime: number = null;			// 通知单张贴时间
  public noticeUploaderName: string = null; // 上传人名字
  public noticeUploaderAccount: string = null; // 上传人账号
  public attachmentIds: string = null;		// 通知单图片IDs, 逗号分隔
  public thumbnailIds: string = null;		// 通知单图片缩略图ID, 逗号分隔
  public electricityMeterIds: string = null;
  public syncStatus: SyncStatus = SyncStatus.None;
  public isSettle: boolean = null; // 有无结清

  public noticeAttachments: Array<Attachment> = [];

  public consumedElectricitys: Array<ElectricityMeter> = [];

  private _id: string = null;

  constructor(json: { [key: string]: any }) {
    super(json);
    Bill.mapFromJSON(json, this);
  }


  addNoticeAttachments(paths: Array<string>): Array<Attachment> {
    let attachments = paths.map((path, index) => {
      let id = new Date().getTime() + '' + (Math.ceil(Math.random() * 1000)) + '' + index;
      let name = path.split('/');
      return new Attachment({id: id , attachmentName: name[name.length - 1], path: path, syncStatus: SyncStatus.None});
    });

    // this.noticeAttachments = this.noticeAttachments.concat( attachments );
    return attachments;
  }


  get hasPostedNotice(): boolean {
    return this.noticeID !== null || this.noticeUploadTime !== null;
  }

  get id(): string {
    if (this._id) {
      return this._id;
    }
    this._id = this.userId + this.year + (this.month > 9 ? this.month : ('0' + this.month));
    return this._id;
  }

  updateAttachmentIdWithNewId(oldAttachmentId: string, newAttachmentId: string) {
    let attachment: Attachment = null;
    if (this.noticeAttachments.some((noticeAttachment, index) => {
      let result = noticeAttachment.id === oldAttachmentId;
      if (result) {
        attachment = noticeAttachment;
      }
      return result;
    })) {
      attachment.id = newAttachmentId;
    }
  }

}

